<?php

class Mithos_Ticket {
    const STATUS_OPENED = 1;
    const STATUS_IN_PROGRESS = 2;
    const STATUS_CLOSED = 3;
    const STATUS_CANCELED = 4;
    
    private $_id;
    private $_author;
    private $_subject;
    private $_status;
    private $_body;
    private $_created;
    private $_attachment;
    private $_visualized;
    
    private $_answers = array();
    private $_data = array();
    
    public function __construct($id = null, $account = null) {
        if ($id !== null) {
            $this->read($id, $account);
        }
    }
    
    public function id($id = null) {
        if ($id === null) {
            return $this->_id;
        }
        $this->_id = $id;
        return $this;
    }
    
    public function author($author = null) {
        if ($author === null) {
            if (!$this->_author instanceof Mithos_Account) {
                return new Mithos_Account($this->_author);
            }
            return $this->_author;
        }
        $this->_author = $author;
        return $this;
    }
    
    public function status($status = null) {
        if ($status === null) {
            return $this->_status;
        }
        $this->_status = $status;
        return $this;
    }

    public function subject($subject = null) {
        if ($subject === null) {
            return $this->_subject;
        }
        $this->_subject = $subject;
        return $this;
    }
    
    public function body($body = null) {
        if ($body === null) {
            return $this->_body;
        }
        $this->_body = $body;
        return $this;
    }
    
    public function created($created = null) {
        if ($created === null) {
            return $this->_created;
        }
        $this->_created = $created;
        return $this;
    }
    
    public function attachment($attachment = null) {
        if ($attachment === null) {
            return $this->_attachment;
        }
        $this->_attachment = $attachment;
        return $this;
    }
    
    public function visualized($visualized = null) {
        if ($visualized === null) {
            return empty($this->_visualized) ? false : (bool)$this->_visualized;
        }
        $this->_visualized = $visualized;
        return $this;
    }
    
    public function answers() {
        $results = Mithos_Mssql::getInstance()->fetchAll('SELECT id 
            FROM Mithos.dbo.ticket_answers 
            WHERE ticket_id = :id[integer]
        ', array('id' => $this->id()));
        foreach ($results as $result) {
            $this->_answers[] = new Mithos_Ticket_Answer($result['id']);
        }
        return $this->_answers;
    }
    
    public function exists() {
        return $this->id() !== null;
    }
    
    public function read($id, $account = null) {
        $result = Mithos_Mssql::getInstance()->fetch('SELECT * 
            FROM Mithos.dbo.tickets
            WHERE id = :id[integer]
        ' . ($account != null ? ' AND author = :author[string]' : ''), array('id' => $id, 'author' => $account));
        if (!empty($result)) {
            $this->_data = $result;
            foreach ($result as $key => $value) {
                if (method_exists($this, $key)) {
                    $this->{$key}($value);
                }
            }
        }
    }
    
    public function findByAccount(Mithos_Account $account) {
        $results = Mithos_Mssql::getInstance()->fetchAll('SELECT * 
            FROM Mithos.dbo.tickets
            WHERE author = :account[string]
            ORDER BY id DESC
        ', array('account' => $account->username()));
        $data = array();
        foreach ($results as $result) {
            $news = new self();
            foreach ($result as $key => $value) {
                if (method_exists($this, $key)) {
                    $news->{$key}($value);
                }
            }
            $data[] = $news;
        }
        return $data;
    }
    
    public function insert() {
        $params = array(
            'author' => $this->author()->username(),
            'subject' => $this->subject(),
            'status' => $this->status(),
            'body' => $this->body()
        );
        
        Mithos_Mssql::getInstance()->query('INSERT INTO Mithos.dbo.tickets 
            (author, subject, status, body, created, visualized) VALUES 
            (:author[string], :subject[string], :status[integer], :body[string], GETDATE(), 1)
        ', $params);
    }
    
    public function update() {
        $params = array(
            'author' => $this->author()->username(),
            'subject' => $this->subject(),
            'status' => $this->status(),
            'body' => $this->body(),
            'visualized' => $this->visualized(),
            'id' => $this->id()
        );

        Mithos_Mssql::getInstance()->query('UPDATE Mithos.dbo.tickets SET 
            author = :author[string], subject = :subject[string], 
            status = :status[integer], body = :body[string], 
            created = GETDATE(), visualized = :visualized[boolean]
            WHERE id = :id[integer]
        ', $params);
    }
}

?>